package org.hackystat.sensor.manual.devtime;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import org.hackystat.sensorshell.SensorShellProperties;
import net.miginfocom.swing.MigLayout;

/**
 * Swing GUI of SensorManager.
 * @author Shaoxuan Zhang
 *
 */
public class HackyReporter extends JFrame {
  /** Support Serialization.*/
  private static final long serialVersionUID = -281860095850212387L;
  
  protected static final String LAYOUT_MODE = //"debug";
                                              "";
  private SensorManager manager;
  
  private ReportPanel reportPanel;
  //private HistoryPanel historyPanel;
  private StatusPanel statusPanel;
  /**
   * Create the GUI and show it.  For thread safety,
   * this method should be invoked from the
   * event-dispatching thread.
   * @param props the properties. make it null to use properties in .hackystat/sensorshell/
   */
  public HackyReporter(SensorShellProperties props) {
    super();
    manager = new SensorManager(props, this);
    setLayout(new MigLayout(LAYOUT_MODE, "[]", "[grow,fill]"));
    JTabbedPane tab = new JTabbedPane();
    add(tab);
    
    reportPanel = new ReportPanel(manager.getReport(), manager);
    reportPanel.setName("ReportPanel");
    tab.add("New Report", reportPanel);  
    HistoryPanel historyPanel = new HistoryPanel(manager);
    historyPanel.setName("HistoryPanel");
    tab.add("History", historyPanel);
    
    //Close on clicking the close button.
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        manager.sendAndQuit();
        System.exit(0);
        }
    });

    //Status panel
    statusPanel = new StatusPanel();
    add(statusPanel, "SOUTH");
    
    //Display the window.
    pack();

    setMinimumSize(getSize());
    setResizable(false);
  }

  /**
   * Launch the application.
   */
/*  public void launch() {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            frameInit();
        }
    });
  }
  */
  /**
   * Run the tool.
   * @param args Command-line Arguments.
   */
  public static void main(String[] args) {
    HackyReporter reporter = new HackyReporter(null);
    reporter.setVisible(true);
    //reporter.launch();
  }

  /**
   * @return the reportPanel
   */
  protected ReportPanel getReportPanel() {
    return reportPanel;
  }

  /**
   * @param message the message to put in status panel.
   */
  public void setStatusMessage(String message) {
    statusPanel.setMessage(message);
    repaint();
  }
}
